<template>
	<div id="doctorPage">
		<shyt-table
			ref="shytTable"
			row-id="id"
			:columns="columns"
			:data="tableData"
			:auto-resize="true"
			:keep-source="true"
			:pager-config="pageConfig"
			:toolbar-config="toolbarConfig"
			:edit-config="{ trigger: 'dbclick', mode: 'row', showStatus: true }"
			:checkbox-config="{
				checkRowKeys: checkRowKeys,
				expandAll: true,
				showHeader: false,
			}"
			@checkbox-change="selectChangeEvent"
			@page-change="pageChange"
		>
			<template #sexCode="{ row }">
				<span>{{ dictModelFun(sexCodeList, row.sexCode) }}</span>
			</template>
			<template #doctorTitle="{ row }">
				<span>{{ dictModelFun(doctorTitleList, row.doctorTitle) }}</span>
			</template>
			<template #doctorPracticeType="{ row }">
				<span>{{
					dictModelFun(doctorPracticeTypeList, row.doctorPracticeType)
				}}</span>
			</template>
			<template #doctorPracticeRange="{ row }">
				<span>{{
					dictModelFun(doctorPracticeRangeList, row.doctorPracticeRange)
				}}</span>
			</template>
			<template #whetherEnable="{ row }">
				<span>{{ dictModelFun(whetherEnableList, row.whetherEnable) }}</span>
			</template>
			<template #toolbar_buttons>
				<el-input
					v-model="doctorInput"
					class="doctor_input"
					placeholder="请输入查找的医生"
					size="medium"
					clearable
				></el-input>
				<el-button type="primary" size="small" @click="handlerSeach">
					查询
				</el-button>
				<el-button type="primary" size="small" @click="handlerAddClick">
					新增
				</el-button>
				<el-button type="primary" size="small" @click="handlerAddWorkClick">
					创建工作组
				</el-button>
			</template>
			<template #edit="{ row }">
				<el-button
					type="text"
					size="small"
					:datalist="row"
					@click="handlerEditRow(row)"
				>
					编辑
				</el-button>
				<el-button type="text" size="small" @click="deldectDoctor(row)">
					删除
				</el-button>
			</template>
		</shyt-table>
		<doctorDialog
			ref="doctorDialog"
			:visible.sync="doctorDialogVisible"
			:title="doctorDialogTitle"
			:doctor-data.sync="curDoctorData"
			:doctor-dialog-type="doctorDialogType"
			:sex-code-list="sexCodeList"
			:doctor-title-list="doctorTitleList"
			:doctor-practice-type-list="doctorPracticeTypeList"
			:doctor-practice-range-list="doctorPracticeRangeList"
			:whether-enable-list="whetherEnableList"
			:dept-list="orgList"
			@submit="handlerSubmit"
		/>
		<doctorWorkDialog
			ref="doctorWorkDialog"
			:visible.sync="doctorWorkDialogVisible"
			:title="doctorWorkDialogTitle"
			:check-row-data="checkRowKeys"
			@submit="handlerWorkSubmit"
		/>
	</div>
</template>
<script>
import {
	doctorInsert,
	doctorUpdate,
	doctorDelete,
	getDoctorPageList,
} from '@/http/doctor'
import { doctorWorkInsert } from '@/http/doctorWork'
import { getOrgList } from '@/http/org'
import { getSysDataDictList } from '@/http/dict'
import doctorDialog from './components/doctorDialog'
import doctorWorkDialog from './components/doctorWorkDialog'
import { mapGetters } from 'vuex'
export default {
	name: 'DoctorPage',
	components: { doctorDialog, doctorWorkDialog },
	data() {
		return {
			checkRowKeys: [],
			doctorWorkDialogVisible: false,
			doctorWorkDialogTitle: '',
			orgList: [],
			sexCodeList: [],
			doctorTitleList: [],
			doctorPracticeTypeList: [],
			doctorPracticeRangeList: [],
			whetherEnableList: [],
			loading: false,
			doctorDialogVisible: false,
			doctorDialogTitle: '',
			curDoctorData: {
				deptId: '',
				jobNumber: '',
				doctorName: '',
				doctorIcon: '',
				sexCode: '',
				nation: '1',
				marriageStatus: '',
				birthCode: '',
				birthdayDate: '',
				countryCode: 'CN',
				idTypeCode: '1',
				idCode: '',
				doctorTelephone: '',
				doctorTitle: '',
				doctorPracticeType: '',
				doctorPracticeRange: '',
				doctorMajor: '',
				briefIntroduction: '',
				whetherEnable: '0',
			},
			doctorDialogType: '',
			doctorInput: null,
			//输入框校验
			rules: {},
			columns: [
				{
					type: 'checkbox',
					width: '50',
				},
				{
					field: 'orgName',
					title: '医院',
				},
				{
					field: 'deptName',
					title: '科室',
				},
				{
					field: 'workName',
					title: '工作组名称',
				},
				{
					field: 'doctorCode',
					title: '医生编号',
				},
				{
					field: 'jobNumber',
					title: '医生工号',
				},
				{
					field: 'doctorName',
					title: '医生名称',
				},
				{
					field: 'sexCode',
					title: '性别',
					slots: { default: 'sexCode' },
				},
				{
					field: 'doctorTelephone',
					title: '联系电话',
				},
				{
					field: 'doctorTitle',
					title: '职称',
					slots: { default: 'doctorTitle' },
				},
				{
					field: 'doctorPracticeType',
					title: '执业类别',
					slots: { default: 'doctorPracticeType' },
				},
				{
					field: 'doctorPracticeRange',
					title: '执业范围',
					slots: { default: 'doctorPracticeRange' },
				},
				{
					field: 'whetherEnable',
					title: '是否启用',
					slots: { default: 'whetherEnable' },
				},
				{
					title: '操作',
					slots: { default: 'edit' },
				},
			],
			tableData: [],
			// 按钮
			toolbarConfig: {
				slots: {
					buttons: 'toolbar_buttons',
				},
			},
			// 分页
			pageConfig: {
				currentPage: 1,
				pageSize: 10,
				pageSizes: [10, 20, 30, 50],
				total: 0,
				background: true,
				layouts: [
					'PrevPage',
					'JumpNumber',
					'NextPage',
					'FullJump',
					'Sizes',
					'Total',
				],
			},
		}
	},
	computed: {
		...mapGetters(['userMsg']),
	},
	async created() {
		this.getOrgList({
			parentUuid: this.userMsg.orgUuid,
			officeType: '2',
			whetherEnable: '0',
			delFlag: '0',
		})
		this.handlerSeach()
		// 性别
		this.sexCodeList = await this.getSysDataDictList('RC_SEX_DICT')
		// 职称
		this.doctorTitleList = await this.getSysDataDictList('STD_REGISTER_TYPE')
		// 职业类别
		this.doctorPracticeTypeList = await this.getSysDataDictList(
			'PRACTICE_CLAS_DICT',
		)
		// 执业范围
		this.doctorPracticeRangeList = await this.getSysDataDictList(
			'RC_PRACTICE_RANGE_DICT',
		)
		this.whetherEnableList = await this.getSysDataDictList('RC_WHETHER_ENABLE')
	},
	methods: {
		selectChangeEvent({ records }) {
			this.checkRowKeys = records
		},
		handlerAddWorkClick() {
			if (!this.checkRowKeys || this.checkRowKeys.length <= 0) {
				this.$message.warning('请选择医生！')
				return
			}
			let row = this.checkRowKeys[0]
			let objArrayList = this.checkRowKeys.filter(
				info =>
					row.id != info.id &&
					row.orgId == info.orgId &&
					row.deptId == info.deptId,
			)
			if (objArrayList.length != this.checkRowKeys.length - 1) {
				this.$message.warning('请选择同医院/科室的医生！')
				return
			}
			this.doctorWorkDialogVisible = true
			this.doctorWorkDialogTitle = '新增工作组'
		},
		async getSysDataDictList(typeCode) {
			let param = { type: typeCode, delFlag: '0', whetherEnable: '0' }
			let list = []
			let res = await getSysDataDictList(param)
			if (res.data?.head?.code == 200) {
				list = res.data.data
			}
			return list
		},
		dictModelFun(dictList, cellVal) {
			let str = '-'
			if (dictList) {
				let obj = dictList.find(e => cellVal == e.value)
				if (obj) {
					str = obj.display
				}
			}
			return str
		},
		getOrgList(param) {
			getOrgList(param).then(({ data }) => {
				if (data?.head?.code == 200) {
					this.orgList = data.data
				}
			})
		},
		// 翻页事件
		pageChange({ currentPage, pageSize }) {
			this.pageConfig.currentPage = currentPage
			this.pageConfig.pageSize = pageSize
			this.handlerSeach()
		},
		// 编辑
		handlerEditRow(row) {
			this.doctorDialogTitle = '医生编辑'
			this.doctorDialogType = '2'
			this.curDoctorData = Object.assign({}, row)
			this.doctorDialogVisible = true
		},
		// 删除
		deldectDoctor(row) {
			this.$confirm('是否删除?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					doctorDelete(row).then(({ data }) => {
						if (data.head.code == 200) {
							this.$message({
								type: 'success',
								message: '删除成功!',
							})
							this.handlerSeach()
						}
					})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除',
					})
				})
		},
		// 新增
		handlerAddClick() {
			this.doctorDialogTitle = '医生新增'
			this.doctorDialogType = '1'
			this.doctorDialogVisible = true
		},
		// 查询
		handlerSeach() {
			let param = {
				pageNum: this.pageConfig.currentPage,
				pageSize: this.pageConfig.pageSize,
				doctorName: this.doctorInput,
			}
			getDoctorPageList(param).then(({ data }) => {
				this.tableData = data.data.records
				this.pageConfig.total = data.data.total
			})
		},
		handlerSubmit(formData, type) {
			let res = { ...formData }
			if (type == '1') {
				doctorInsert(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('新增成功')
						this.doctorDialogVisible = false
						this.handlerSeach()
					}
				})
			} else {
				doctorUpdate(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('编辑成功')
						this.doctorDialogVisible = false
						this.handlerSeach()
					}
				})
			}
		},
		handlerWorkSubmit(formData) {
			let res = { ...formData }
			doctorWorkInsert(res).then(({ data }) => {
				if (data?.head?.code == 200) {
					this.$message.success('新增成功')
					this.doctorWorkDialogVisible = false
					this.handlerSeach()
					this.checkRowKeys = []
				}
			})
		},
	},
}
</script>

<style lang="scss" scoped>
.doctor_input {
	width: 200px;
}
</style>
